package com.itheima.utils;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class DownLoadUtil {
    /**
     * @param name  表格名称
     * @param title 表头
     * @param list  数据集合
     * @throws IOException
     */
    public static void writeExcel(String name, String[] title, List<?> list) throws Exception {
        HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheet = sheets.createSheet("哈哈");
        HSSFRow headRow = sheet.createRow(0);
        //1 解析表头 并放入excel的第一行
        for (int i = 0; i < title.length; i++) {
            headRow.createCell(i).setCellValue(title[i]);
        }

        //2 遍历list
        for (int i = 0; i < list.size(); i++) {
            //2.1 解析数据 获取所有的方法 然后遍历 拼接
            //获取里面所有的成员变量
            Object o = list.get(i);
            Field[] fields = list.get(i).getClass().getDeclaredFields();
//            fields.setAccessible(true);
            //遍历成员变量
            for (int j = 0; j < fields.length; j++) {
                //进行拼接
                Method declaredMethod = list.get(j).getClass().getDeclaredMethod(
                        "get" + fields[j].getName().substring(0, 1).toUpperCase() + fields[j].getName().substring(1));

                System.out.println(declaredMethod);


            }

        }

        FileOutputStream fos = new FileOutputStream("E:\\health_parent\\health_parent\\daochu.xlsx");
        sheets.write(fos);
        fos.flush();
        fos.close();
        sheets.close();
    }
}
